<?php
/**
 * 基础物料的队列任务导入最终执行Lib类
 *
 * @author kamisama.xia@gmail.com 
 * @version 0.1
 */

class material_basic_material_to_import {

    /**
     * 基础物料关联条码的队列任务执行
     * 
     * @param String $cursor_id
     * @param Array $params
     * @param String $errmsg
     * @return Boolean
     */
    function run(&$cursor_id,$params,&$errmsg){
        $importObj = app::get($params['app'])->model($params['mdl']);
        $barcodeObj = app::get('material')->model('barcode');
        $basicMaterialExtObj = app::get('material')->model('basic_material_ext');
        $basicMaterialFeatureGrpObj = app::get('material')->model('basic_material_feature_group');
        $basicMaterialStockObj = app::get('material')->model('basic_material_stock');
        $basicMaterialConfObj = app::get('material')->model('basic_material_conf');
        $dataSdf = $params['sdfdata'];

        foreach ($dataSdf as $v){
            $importData = array(
                'material_name' => $v[0],
                'material_bn' => $v[1],
                'material_bn_crc32' => sprintf('%u',crc32($v[1])),
                'type' => $v[2],
                'visibled' => $v[4],
                'create_time' => time(),
            );
          
            $is_save = $importObj->save($importData);
            if($is_save){
                //保存条码信息
                $sdf = array(
                    'bm_id' => $importData['bm_id'],
                    'type' => material_codebase::getBarcodeType(),
                    'code' => $v[3],
                );
                $barcodeObj->insert($sdf);

                //保存保质期配置
                $useExpireConfData = array(
                    'bm_id' => $importData['bm_id'],
                    'use_expire' => $v[10] == 1 ? 1 : 2,
                    'warn_day' => $v[11] ?  $v[11] : 0,
                    'quit_day' => $v[12] ? $v[12] : 0,
                    'create_time' => time(),
                );
                $basicMaterialConfObj->save($useExpireConfData);

                //如果是组合物料保存相关数据
                if($v[2] == 2){
                    $basicMaterialCombinationItemsObj = app::get('material')->model('basic_material_combination_items');
                    foreach($v[9] as $k=>$num){
                        $tmpChildMaterialInfo = $importObj->dump($k, 'material_name,material_bn');

                        $addCombinationData = array(
                            'pbm_id' => $importData['bm_id'],
                            'bm_id' => $k,
                            'material_num' => $num,
                            'material_name' => $tmpChildMaterialInfo['material_name'],
                            'material_bn' => $tmpChildMaterialInfo['material_bn'],
                            'material_bn_crc32' => sprintf('%u',crc32($tmpChildMaterialInfo['material_bn'])),
                        );
                        $basicMaterialCombinationItemsObj->insert($addCombinationData);
                        $addCombinationData = null;
                    }
                }

                //保存基础物料的关联的特性
                //to do 暂时去掉这块逻辑，有待实现

                //保存物料扩展信息
                $addExtData = array(
                    'bm_id' => $importData['bm_id'],
                    'cost' => $v[7] ? $v[7] : 0.00,
                    'retail_price' => $v[6] ? $v[6] : 0.00,
                    'weight' => $v[8] ? $v[8] : 0.00,
                    'unit' => $v[5],
                    'cat_id' => $v[13] ? $v[13] : '',
                    'specifications' => $v[14] ? $v[14] : '',
                    'brand_id' => $v[15] ? $v[15] : '',
                );
                $basicMaterialExtObj->insert($addExtData);
                
                //保存物料库存信息
                $addStockData = array(
                    'bm_id' => $importData['bm_id'],
                    'store' => 0,
                    'store_freeze' => 0,
                );
                $basicMaterialStockObj->insert($addStockData);

            }else{
                $m = $importObj->db->errorinfo();
                if(!empty($m)){
                    $errmsg.=$m.";";
                }
            }
         }
         
        return false;
    }
}
